//package cn.iocoder.yudao.module.system.controller.admin.mini.back.controller.admin.back;
//
//import cn.iocoder.yudao.framework.common.exception.ServiceException;
//import cn.iocoder.yudao.framework.common.pojo.CommonResult;
//import cn.iocoder.yudao.module.system.controller.admin.mini.back.dal.dataobject.back.BackDO;
//import cn.iocoder.yudao.module.system.controller.admin.mini.back.dal.mysql.back.BackMapper;
//import cn.iocoder.yudao.module.system.controller.admin.mini.order.dal.dataobject.order.OrderDO;
//import cn.iocoder.yudao.module.system.controller.admin.mini.order.dal.mysql.order.OrderMapper;
//import com.github.wxpay.sdk.WXPayUtil;
//import org.apache.http.HttpEntity;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.ssl.SSLContexts;
//import org.apache.http.util.EntityUtils;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.annotation.Resource;
//import javax.net.ssl.SSLContext;
//import java.io.File;
//import java.io.InputStream;
//import java.nio.file.Files;
//import java.security.KeyStore;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.UUID;
//
////退款
//@RestController
//public class PayBackController {
//
//    @Resource
//    OrderMapper orderMapper;
//    @Resource
//    BackMapper backMapper;
//
////    @ApiOperation("退款")
//    @PostMapping(value = "/refund")
//    public CommonResult refund(@RequestBody IdsVo vo) throws Exception {
//        BackDO backDO = backMapper.selectById(vo.getId());
//        if (backDO==null)
//            throw new ServiceException(500,"退货单不存在,请刷新");
//        OrderDO orderDO = orderMapper.selectById(backDO.getOrderId());
//        if (orderDO==null)
//            throw new ServiceException(500,"订单不存在");
//        //构建参数
//        Map<String, String> dataMap = new HashMap<>();
//        //小程序ID
//        dataMap.put("appid", WxLoginVo.appid);
//        //商户号
//        dataMap.put("mch_id", WxLoginVo.mchId);
//        //随机字符串
//        dataMap.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
//        //微信订单号
//        dataMap.put("out_trade_no", orderDO.getOrderCode());
//        //商户退款单号
//        String outRefundNo = "NO" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//        dataMap.put("out_refund_no", outRefundNo);
//        //订单金额
//        dataMap.put("total_fee", "1");
//        //退款金额
//        dataMap.put("refund_fee", "1");
//        //商户证书密钥，生成签名
//        String sign = WXPayUtil.generateSignature(dataMap, WxLoginVo.mchKey);
//        dataMap.put("sign", sign);
//        refound(dataMap);
//        return CommonResult.success("");
//    }
//
//    public static void refound(Map<String, String> dataMap) {
//        try {
//            //将传来的map转xml
//            StringBuffer sb = new StringBuffer();
//            sb.append("<xml>");
//            for (String key : dataMap.keySet()) {
//                String value = "<![CDATA[" + dataMap.get(key) + "]]>";
//                sb.append("<" + key + ">" + value + "</" + key + ">");
//                System.out.println();
//            }
//            sb.append("</xml>");
//            String xmlString = sb.toString();
//            doRefoud(dataMap.get("mch_id"), "https://api.mch.weixin.qq.com/secapi/pay/refund", xmlString);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//    private static String doRefoud(String mch_id, String url, String xmlString) throws Exception {
//
//        //证书 是从微信商户平台-》账户设置-》 API安全 中下载的
//        KeyStore keyStore = KeyStore.getInstance("PKCS12");
//        //我想的是把证书传到服务器上，没写出来
//        InputStream inputStream = Files.newInputStream(new File(WxLoginVo.keyPath).toPath());
//        try {
//            //这里写密码，默认是MCHID
//            keyStore.load(inputStream, WxLoginVo.mchId.toCharArray());
//        } finally {
//            inputStream.close();
//        }                                                           //这里写密码
//        SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, WxLoginVo.mchId.toCharArray()).build();
//        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
//        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
//
//        try {
//            //请求路径
//            HttpPost httpPost = new HttpPost(url);
//            StringEntity stringEntity = new StringEntity(xmlString, "UTF-8");
//            System.err.println("aaa:"+stringEntity);
//            httpPost.setEntity(stringEntity);
//            CloseableHttpResponse response = httpClient.execute(httpPost);
//            try {
//                HttpEntity entity = response.getEntity();
//                //接受到返回信息
//                String jsonStr = EntityUtils.toString(response.getEntity(),"UTF-8");
//                System.err.println(jsonStr);
//                EntityUtils.consume(entity);
//            } finally {
//                response.close();
//            }
//        } finally {
//            httpClient.close();
//        }
//        return null;
//    }
//
//
//
//
//
//    public static void main(String[] args) throws Exception {
//        Map<String, String> map = new HashMap<>();
//        map.put("appid","wx13987d641abc9d0e");
//
//        map.put("mch_id","1645826330");
//
//        map.put("nonce_str","bc065d21a7914000b1f309c63560b788");
//
//        map.put("out_refund_no","NO20230608152216");
//        map.put("out_trade_no","QW20230608151104");
//
//        map.put("refund_fee","1");
//
//        map.put("total_fee","1");
////        map.put("transaction_id","4200001852202306086082417281");
//
//        String sign = WXPayUtil.generateSignature(map, WxLoginVo.mchKey);
//        map.put("sign",sign);
//        refound(map);
//
////        String aaa="appid=wx13987d641abc9d0e&mch_id=1645826330&nonce_str=bc065d21a7914000b1f309c63560b788&out_refund_no=NO20230608152216&out_trade_no=QW20230608151104&refund_fee=1&total_fee=1&key=1DAf906d12a044d99798933e50c3asdw";
////        String s = Md5Util.md5(aaa);
////
////        System.err.println("sign:"+sign);
////        System.err.println("s:"+s);
//    }
//
//
//
//}
